package xml;
import java.io.File;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/** 
 * Project: RovioIsWatchingYou
 * File: XML_handler.java
 *
 * Description: Vorlage fuer die weiteren XML-handler
 *
 * last change: 20.06.2012
 * 
 * @author ebaerj
 */

public class XML_handler {

	protected Document document;
    private DocumentBuilderFactory factory;
    private DocumentBuilder builder;
    private File f;
    
    /**welches XMl wird ausgelesen?*/
    private String which;
    
    protected Vector<String> attributeValues=new Vector<String>();
	protected NodeList liste;
    
	
    public XML_handler() throws ParserConfigurationException{
    	//Erstellt Datenbild in Java
        factory = DocumentBuilderFactory.newInstance();
 
  
		builder = factory.newDocumentBuilder();
		
        
    }
    
    
    /**
     * @param id ID des Objektes, das geaendert werden soll
     * @param attribute Eigenschaft des Objektes, welche geaendert werden soll
     * @param value Wert, den die Eigenschaft neu annehmen soll
     * @return true, falls etwas geaendert wurde, false, falls kein passendes Objekt gefunden wurde
     */
    public Boolean changeValue(String id, String attribute, String value){
    	NodeList liste=document.getElementsByTagName(which);
    	for (int i=0;i<liste.getLength();i++){
    		if (liste.item(i).getAttributes().getNamedItem("id").getNodeValue().equals(id)){
    			liste.item(i).getAttributes().getNamedItem(attribute).setNodeValue(value);
    			save();
    			return true;
    		}
    	}
  
    	return false;
    }
    
    
    
    /**
     * Speichert die Aenderungen, welche am XML vorgenommen wurden, ab
     */
    public void save() {
        // DOM Dokument zum Schreiben vorbereiten
        Source source = new DOMSource(document);
        // Outputfile vorbereiten

        StreamResult result = new StreamResult(f);
        // DOM Dokument schreiben
        Transformer xformer;
        try {
            xformer = TransformerFactory.newInstance().newTransformer();
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            xformer.setOutputProperty(OutputKeys.METHOD, "xml");
            xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
            xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            xformer.transform(source, result);
        } catch (TransformerFactoryConfigurationError | TransformerException e) {
            e.printStackTrace();
        }
    }
    
    //------------------- Getters & Setters ------------------- 
    
	public File getFile() {
		return f;
	}
	
	public void setFile(File f) {
			this.f = f;
	}
	
	public DocumentBuilder getBuilder() {
		return builder;
	}


	public void setWhich(String which) {
		this.which = which;
	}

	public void setDocument(Document document) {
		this.document = document;
	}
	
	public Document getDocument() {
		return document;
	}

}
